<?php
	namespace Admin\Controller;
	use Common\Controller\PublicController;
	header("Content-Type:text/html;charset=utf-8");
	class DealController extends PublicController
	{
		//查看协议
		public function deal_list()
		{
			//查询协议
			$m = M("agreement");
			$arr = $m->select();
			
			$this->assign("arr", $arr);
			$this->display();
		}
		//添加协议界面
		public function deal_add()
		{
			$this->display();
		}
		//添加协议
		public function add_deal()
		{
			//获得协议标题和内容
			$id = session('admin_id');
			$_POST['id'] = $id;
			$myfile = $_FILES['myFile'];
			if ($myfile['name'] != NULL)
			{
				//文件上传
				$upload = new \Think\Upload();// 实例化上传类    
				$upload->maxSize   =     3145728 ;// 设置附件上传大小    
				$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg','txt');// 设置附件上传类型
				$filename = $myfile['name'];//获得文件名
				$index = strrpos($filename, ".");//对文件名进行“.”截取
				$ext = substr($filename, $index);//获得文件的扩展名
				$newname = md5(date("ymdHis").mt_rand(1000, 9999)).$ext;//重新命名该文件
				$upload->saveName = $newname;
				$upload->autoSub = false;//关闭使用子目录保存
				$upload->rootPath = './Public/Uploads/agreement/'; // 设置附件上传根目录  
				/* $upload->savePath = './agreement/'.$newname; */ // 设置附件上传（子）目录      
				// 上传单个文件     
				$info   =   $upload->upload();    
				if(!$info) 
				{
					// 上传错误提示错误信息        
					$this->error($upload->getError()); 
				}
				else
				{
					$_POST['savepath'] = $upload->saveName;//将文件存储的路径名放入Post中
					$result = M("agreement")->add($_POST);//向agreement表中添加带有文件的协议记录
					if ($result > 0)
					{
						$this->success("协议添加成功！", U("Deal/deal_add"));
					}
					else
					{
						$this->error("协议添加失败！", U("Deal/deal_add"));
					}
				}
			 }	
		}
		//修改协议
		public function dealModify()
		{
			//获得要修改协议的id
			$id = $_GET['id'];
			//查询id对应下的协议
			$agreement = M("agreement")->where("id={$id}")->find();
			
			$this->assign("agreement", $agreement);
			$this->display();
		}
		//删除协议
		public function dealDelete()
		{
			//获得要删除的协议的id
			$aid = $_GET['id'];
			//删除该id对应的协议
			$res = M("agreement")->where("aid={$aid}")->delete();
			
			if ($res) 
			{
				$this->success("删除协议成功！", U("Deal/deal_list"));
			}
			else
			{
				$this->error("删除协议失败！", U("Deal/deal_list"));
			}
		}
	}